package com.finddeer.bookkeeping.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.finddeer.bookkeeping.Bill;
import com.finddeer.bookkeeping.BillLab;
import com.finddeer.bookkeeping.R;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * author    : leaves
 * gitee     : https://gitee.com/LEAVES2018
 * time      : 2018/12/23 8:19 PM
 * function  :
 */
public class StatisticsFragment extends Fragment {

    private float[] mBillTemp; //暂存bill的amount
    private String[] str = {"餐饮", "交通", "娱乐", "学习", "购物", "医疗", "投资", "情意"};
    PieChart pieChart;

    public void updatePieChart() {
        List<PieEntry> entries = new ArrayList<>();

        BillLab billLab = BillLab.get(getActivity());
        List<Bill> bills = billLab.getBills();

        mBillTemp = new float[str.length];

        for (Bill bill : bills) {
            for (int i = 0; i < str.length; i++) {
                if (bill.getTag().contentEquals(str[i])) {  //  如果bill属于该类别
                    mBillTemp[i] += (float) bill.getAmount();   //  该类别总额加上bill的amount值
                    break;
                }
            }
        }

        for (int i = 0; i < str.length; i++) {
            if(mBillTemp[i] == 0) {
                continue;
            }

            entries.add(new PieEntry(mBillTemp[i], str[i]));
        }


        ArrayList<Integer> colors = new ArrayList<>();
        // 饼图颜色
        colors.add(Color.rgb(111, 16, 76));
        colors.add(Color.rgb(156, 122, 144));
        colors.add(Color.rgb(120, 169, 169));
        colors.add(Color.rgb(215, 196, 187));
        colors.add(Color.rgb(181, 124, 103));
        colors.add(Color.rgb(215, 84, 85));
        colors.add(Color.rgb(225, 166, 121));
        colors.add(Color.rgb(251, 226, 81));

        PieDataSet set = new PieDataSet(entries,"");
        PieData data = new PieData(set);
        set.setColors(colors);
        set.setSliceSpace(0f);
        set.setSliceSpace(3f);//设置饼块之间的间隔
        set.setSelectionShift(5f);//设置饼块选中时偏离饼图中心的距离
        pieChart.setData(data);
        pieChart.setNoDataText("我需要数据");
        data.setValueTextSize(18f);
        pieChart.setCenterText("消费统计");
        pieChart.setCenterTextSize(22f);
        pieChart.invalidate(); // refresh
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_statistics, container, false);

        pieChart = view.findViewById(R.id.pieChart);
        updatePieChart();

        return view;
    }
}
